package com.fau.wm.action.index;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.wm.action.PublicAction;
import com.fau.wm.entity.MedicalAdmin;
import com.fau.wm.entity.MedicalOrdersAlreadyDelivery;
import com.fau.wm.util.FileNameEncoderUtil;
import com.fau.wm.util.SystemConfig;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 订单退单Action
 * @author kangyj
 * @datetime 2014年11月29日,上午1:09:32
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class OrdersRetreatAction extends PublicAction implements
		ModelDriven<MedicalOrdersAlreadyDelivery> {
	
	private static final long serialVersionUID = -6937898251488699160L;
	
	private MedicalOrdersAlreadyDelivery ordersAlreadyDelivery = new MedicalOrdersAlreadyDelivery();
	
	/**
	 * @desc 订单退单页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月28日,下午8:42:47
	 */
	@Action(value = "orders_retreat_init", results = {@Result(name = SUCCESS, location = "orders_retreat_init.jsp")})
	public String init() {
		return SUCCESS;
	}
	
	/**
	 * @desc 分页查询已发货订单
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月28日,下午10:10:05
	 */
	@Action(value = "orders_retreat_view")
	public String view() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		ordersAlreadyDelivery.setHospitalId(admin.getEnterpriseId());
		pageBean = this.baseAllService.getOrdersManagerService().findOrdersAlreadyDelivery(ordersAlreadyDelivery, map);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 订单批量退单
	 * @return null
	 * @author kangyj
	 * @datetime 2014年11月29日,上午1:54:47
	 */
	@Action(value = "orders_retreat")
	public String retreat() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(ordersAlreadyDelivery.getId())) {
			this.baseAllService.getOrdersManagerService().ordersRetreat(ordersAlreadyDelivery.getId());
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 订单批量网退
	 * @return null
	 * @author liy
	 * @datetime 2014年12月13日,下午4:20:10
	 */
	@Action(value = "orders_net_retreat")
	public String netRetreat() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(ordersAlreadyDelivery.getId())) {
			this.baseAllService.getOrdersManagerService().ordersNetRetreat(ordersAlreadyDelivery.getId());
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 导入退单页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月29日,上午12:09:33
	 */
	@Action(value = "orders_retreat_import_init", results = {@Result(name = SUCCESS, location = "orders_retreat_import_init.jsp")})
	public String importDeliveryInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 导入退单
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月29日,上午12:10:55
	 */
	@Action(value = "orders_retreat_import")
	public String importDelivery() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		map = new HashMap<String, Object>();
		File file = this.getImportFile(request);
		try {
			String row = this.baseAllService.getOrdersManagerService().importRetreat(file);
			String rowCount[] = row.split(",");
			map.put("rightCount", rowCount[0]);
			map.put("errorCount", rowCount[1]);
			map.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 下载批量退单模版
	 * @return null
	 * @author liy
	 * @datetime 2014年12月26日,下午12:29:28
	 */
	@Action(value = "orders_retreat_download")
	public String medicineDownload() {
		this.download(SystemConfig.MEDICINE_RETREAT_MASTERPLATE, "批量导入退单.xls");
		return null;
	}
	
	/**
	 * @desc 订单退单-导出当前页
	 * @return null
	 * @author liy
	 * @datetime 2014年12月28日,下午5:05:24
	 */
	@Action(value = "orders_retreat_export_current")
	public String currentExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"订单退单导出.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getOrdersManagerService().exportRetreatOrdersCurrent(this.ordersAlreadyDelivery.getId());
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 订单退单-导出全部
	 * @return null
	 * @author liy
	 * @datetime 2014年12月28日,下午5:35:21
	 */
	@Action(value = "orders_retreat_export_all")
	public String allExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"订单退单导出.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getOrdersManagerService().exportRetreatOrdersAll(this.ordersAlreadyDelivery);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public MedicalOrdersAlreadyDelivery getModel() {
		return ordersAlreadyDelivery;
	}

}
